package com.wsoft.bpm.component;

import cn.hutool.core.date.DateUtil;
import com.wsoft.bpm.constants.BpmConstant;
import lombok.extern.log4j.Log4j2;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 定时任务
 *
 * @Author zhonglj
 * @Date 2024-06-05 17:46
 */
@Log4j2
@Component
public class BpmTask {
    @Resource
    private TaskService taskService;

    /**
     * 定时关闭任务
     *
     * @param
     * @return void
     * @author zhonglj
     * @date 2024-09-24 15:36
     **/
    @Scheduled(cron = "0 0/1 * * * ?")
    public void completeOverdueTasks() {
        List<Task> overDueTasks = taskService.createTaskQuery()
                // 查询所有 dueDate 在当前时间之前的任务
                .taskDueBefore(DateUtil.date())
                // 审批中
                .active()
                .list();

        for (Task task : overDueTasks) {
            taskService.addComment(task.getId(), task.getProcessInstanceId(), BpmConstant.AUTO_COMPLETE_COMMENT);
            taskService.complete(task.getId());
        }
    }
}
